<template>
  <div class="adduserBody">
    <div style="text-align: center">
      <h3>新增用户</h3>
    </div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" size="small">
      <el-form-item label="登录名" prop="code">
        <el-input v-model="ruleForm.code"></el-input>
      </el-form-item>
      <el-form-item label="用户姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="emailNumber">
        <el-input v-model="ruleForm.emailNumber"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model="ruleForm.phoneNumber"></el-input>
      </el-form-item>
      <el-form-item label="用户性别" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import axios from 'axios'
  import QS from 'qs';
  import { getToken, setToken, removeToken } from '@/utils/auth'


  export default {
      name: "adduser",

      data() {
        return {
          ruleForm: {
            code: '',
            name: '',
            password: '',
            // againPassword: '',
            emailNumber: '',
            phoneNumber: '',
            region: '',
            resource: '',
          },
          rules: {
            code: [
              { required: true, message: '请输入登录名', trigger: 'blur' },
              { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
            ],
            name: [
              { required: true, message: '请输入用户名称', trigger: 'blur' },
            ],
            password: [
              { required: true, message: '请输入登录密码', trigger: 'blur' },
              { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
            ],
            // againPassword: [
            //   { required: true, message: '请进行密码确认', trigger: 'blur' },
            //   { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
            // ],
            emailNumber: [
              { required: true, message: '请输入邮箱地址', trigger: 'blur' },
              { type: 'email', message: '请输正确的邮箱地址', trigger: 'blur' }
            ],
            phoneNumber: [
              { required: true, message: '请输入手机号码', trigger: 'blur' },
              { min: 11, max: 11, message: '请输入正确的手机号码', trigger: 'blur' }
            ],
            resource: [
              { required: true, message: '请选择用户性别', trigger: 'change' }
            ],
          }
        };
      },
      methods: {

        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            const that = this;   //由于this指向已经不是 vue 的对象啦，所以必须重新对this进行定义。
            if (valid) {
              const userform = {};
              userform['userCode'] = this.ruleForm.code
              userform['userName'] = this.ruleForm.name;
              userform['userPwd'] = this.ruleForm.password;
              userform['userEmail'] = this.ruleForm.emailNumber;
              userform['userPhone'] = this.ruleForm.phoneNumber;
              userform['userGender'] = this.ruleForm.resource;
              axios.post('/api/sys/sysUser/addSysUser',QS.stringify(userform)) //使用QS对对象进行包裹
              .then(function (response) {
                const data = response.data
                if (data.code == "200"){
                  alert('添加成功');
                  that.$router.push({ path: '/user2' });
                }else if (data.code == "500"){
                  if (data.errorMessage='用户编码不能重复'){
                    alert('登录名已存在，请重新输入登录名!');
                    that.ruleForm.code='';
                  }else{
                    alert(data.errorMessage);
                  }
                }else{
                  alert('错误代码:' + data.code + '请联系系统管理员！')
                }
              })
              .catch(function (error) {
                console.log(error);
              });

            }else {
              console.log('error submit!!');
              return false;
            }
          });
        },

        resetForm(formName) {
          this.$refs[formName].resetFields();
        }
      }
    }
</script>

<style scoped>
  .adduserBody{
    margin: 10px 10px;
    width: 600px;
  }

</style>
